<?php

abstract class Base_Controller
{
	private $data = array();
	private $dispatch_class;

	private $model = null;
	private $view = null;

	public function __construct()
	{
		$this->getData();
	}

	public function getData()
	{
		$this->data = $_REQUEST;	//hmm... don't know if i like this.
		$this->parseUrl( );	
	}

	private function parseUrl()
	{
	
		// use php to parse the url into domain, protocol, path, etc.
		$pieces = parse_url( $_SERVER['REQUEST_URI']);

		//strip the trailing slash if it exists.
		$path = $pieces['path'];
		$path = chop( $path, '/');

		$tmp = explode('/', $path);

		//use the last folder as the class to load.
		$this->data['dispatch_class'] = strtolower(array_pop($tmp));

		//if it's the base path, just use gear as destination.
		if ( $this->data['dispatch_class'] == 'lotrogear')
		{
			$this->data['dispatch_class'] = 'gear';
		}

	}	//end of parseUrl()
	
	
	public function dispatch()
	{
		$model = $this->data['dispatch_class'] . '_model';
		$view = $this->data['dispatch_class'] . '_view';

		if (!class_exists( $model, true) || !class_exists($view, true))
		{
			echo 'bad url';
			exit();
		}

		$this->model = new $model( $this->data );
		$this->view = new $view();
		$this->view->setData( $this->model->getData());

		$this->view->doView();
	}	//end of dispatch()
	
	
	

}	//end of class